package service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mapper.MedicineMapper;
import entity.MedicineEntity;
import org.springframework.stereotype.Service;

@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, MedicineEntity> implements MedicineService {

    @Override
    public MedicineEntity getMedicineById(Integer id) {
        return getById(id);
    }

    @Override
    public IPage<MedicineEntity> getAllMedicines(IPage<MedicineEntity> page) {
        return page(page);
    }

    public IPage<MedicineEntity> searchMedicinesFuzzy(String keyword, IPage<MedicineEntity> page) {
        LambdaQueryWrapper<MedicineEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(MedicineEntity::getName, keyword);
        return page(page, queryWrapper);
    }

    @Override
    public boolean addMedicine(MedicineEntity medicine) {
        return save(medicine);
    }

    @Override
    public boolean deleteMedicine(Integer id) {
        return removeById(id);
    }

    @Override
    public boolean updateMedicine(MedicineEntity medicine) {
        LambdaQueryWrapper<MedicineEntity> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(MedicineEntity::getId, medicine.getId());
        return update(medicine, updateWrapper);
    }
}